///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_OS_CICONTHEME_H__
#define __BUOLA_OS_CICONTHEME_H__

#include <buola/io/curi.h>
#include <unordered_set>
#include <set>

namespace buola { namespace os {

///\addtogroup gui
///@{

////////////////////////////////////////////////////////////////////////////

/// icon theme

///
///
///
////////////////////////////////////////////////////////////////////////////

class CIconTheme
{
public:
    enum EContext
    {
        CONTEXT_ANY,
        CONTEXT_ACTIONS,
        CONTEXT_DEVICES,
        CONTEXT_FILESYSTEMS,
        CONTEXT_MIMETYPES
    };

    enum EType
    {
        TYPE_FIXED,
        TYPE_SCALABLE,
        TYPE_THRESHOLD
    };

protected:
    //! path of a folder containing icons
    struct SPath
    {
        io::CURI mPath;
        std::unordered_set<std::string> mFiles;
    };

    //! folder containing icons
    struct SDir
    {
        SDir(const std::string &pName);

        void InitPaths();
        bool FindIcon(const std::string&,io::CURI&);
        bool MatchesSize(int);
        int GetDistance(int);

        std::string mName;
        int mSize;
        EContext mContext;
        EType mType;
        int mMaxSize;
        int mMinSize;

        bool mPathsInit;
        std::vector<std::unique_ptr<SPath>> mPaths;
    };

//! \name constructor and destructor
//@{
private:
    CIconTheme(const std::string &pName);
public:
    ~CIconTheme();
//@}

private:
    void Inherit(const std::string &pName);

public:
    //! returns true if some icon with the given name exists in the theme
    bool HasIcon(const std::string &pName,EContext pContext=CONTEXT_ANY,bool pCheckInherited=true);

    //! returns the icon which is a best match, by first checking ALL the icons in this theme, and then checking in
    //! inherited themes.
    std::string GetFirst(const std::vector<std::string> &pNames,EContext pContext=CONTEXT_ANY,bool pCheckInherited=true);

    //! returns an image for the given icon
    io::CURI GetIcon(const std::string &pName,int &pSize,EContext pContext=CONTEXT_ANY,bool pCheckInherited=true);

    //! returns the icon theme of the given name
    static CIconTheme *Get(const std::string &pName);

private:
    std::string mName;
    std::wstring mVisibleName;
    std::wstring mComment;
    bool mHidden;
    std::string mExample;
    std::set<std::string> mInheritSet;
    std::vector<CIconTheme*> mInherits;

    std::vector<std::unique_ptr<SDir>> mDirs;

    static std::map<std::string,CIconTheme*> mThemes;

    friend void checked_delete(CIconTheme*);
};

///@}

/*namespace os*/ } /*namespace buola*/ }

#endif
